package org.huangrui.juc.source.cas;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @Author hr
 * @Create 2024-04-04 17:02
 */
public class T04_AtomicStamped {
    public static void main(String[] args) {
        Book java = new Book(1, "java");
        AtomicStampedReference<Book> reference = new AtomicStampedReference<>(java,1);
        int stamp = reference.getStamp();
        System.out.println(reference.getReference()+"\t" + reference.getStamp());
        Book mysql = new Book(2, "mysql");
        boolean b;
        b = reference.compareAndSet(java, mysql, reference.getStamp(), reference.getStamp() + 1);
        System.out.println(b+"\t"+reference.getReference()+"\t" + reference.getStamp());
        b = reference.compareAndSet(mysql,java, reference.getStamp(), reference.getStamp()+1);
        System.out.println(b+"\t"+reference.getReference()+"\t" + reference.getStamp());
        b = reference.compareAndSet(java, mysql, stamp, stamp + 1);
        System.out.println(b+"\t"+reference.getReference()+"\t" + reference.getStamp());

    }
}
@NoArgsConstructor
@AllArgsConstructor
@Data
class Book{
    private int id;
    private String bookName;
}